package com.zheng.interceptor;

import com.zheng.util.CookieUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  @author: zheng
 *  @Date: 2020-06-13 19:35
 *  @Description: 访问拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断cookie中是否有值，么有则跳转登陆页面
        String token = CookieUtils.getCookieByName(request, "token");
        if(StringUtils.isEmpty(token)){
            response.sendRedirect("http://localhost:8080/");  //跳转到登录页面
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
